package com.jinyun.utils;

import com.jinyun.vo.LocationPoint;

import java.util.List;

public class PolygonUtils {

    /**
     * 判断点是否在多边形内（射线法）
     *
     * @param lat  点的纬度
     * @param lng  点的经度
     * @param points 多边形的顶点坐标列表
     * @return 如果点在多边形内部则返回true，否则返回false
     */
    public static boolean isPointInPolygon(String lat, String lng, List<LocationPoint> points) {
        double testx = Double.parseDouble(lng);
        double testy = Double.parseDouble(lat);
        int j = points.size() - 1;
        boolean oddNodes = false;

        for (int i = 0; i < points.size(); i++) {
            LocationPoint pi = points.get(i);
            LocationPoint pj = points.get(j);

            double xi = Double.parseDouble(pi.getLng());
            double yi = Double.parseDouble(pi.getLat());
            double xj = Double.parseDouble(pj.getLng());
            double yj = Double.parseDouble(pj.getLat());

            if ((yi < testy && yj >= testy || yj < testy && yi >= testy)
                    && (xi + (testy - yi) / (yj - yi) * (xj - xi) < testx)) {
                oddNodes = !oddNodes;
            }
            j = i;
        }
        System.out.println("oddNodes = " + oddNodes);
        return oddNodes;
    }
}